Int_t AAFProofUtils() {
  Printf("Usage : ");
  Printf("\troot[] gROOT->LoadMacro(\"AAFProofUtils.C\");");
  Printf("\troot[] RunFullProccesFromAliEnToDataSet()");
  Printf("");
  
  return 0;
}

Int_t CopyDataSetToDifferentSE(TString dsName="/COMMON/COMMON/MC_LHC10a14_104824",TString newDsName="LHC09d_000104800_p4_SE",TString proof="aliceskaf@skaf.saske.sk",TString redirector="ali02-iep-grid.saske.sk",Bool_t useFastDownload=kTRUE,TString zipfile="root_archive.zip",TString rootfilename="#AliESDs.root",TString defaultTree="esdTree") {
  
  Int_t retval=0;
  TString tmpTxt= Form("tmp_%s.txt",newDsName.Data());
  if (CreateTxtFromDataSet(tmpTxt,dsName,proof,redirector)) return 1;
  
  // only OK files are chcecked last time
  TString tmpOutputTxt_OK= tmpTxt.Data();
  
  if (useFastDownload) {
    Info("CopyDataSetToDifferentSE","OpenFilesMain(async) ...");
    Int_t retVal = OpenFilesMain(tmpTxt.Data(),"async");
    if (retVal) { Error("RunFullProccesFromAliEnToDataSet","Error in OpenFilesMain(\"async\") !!!"); return retVal; }
    tmpOutputTxt_OK.ReplaceAll(".txt","");
    tmpOutputTxt_OK.Append("_OK.txt");

  }

  Info("CopyDataSetToDifferentSE","OpenFilesMain(Normal) ...");
  retVal = OpenFilesMain(tmpOutputTxt_OK.Data(),"normal");
  if (retVal) { Error("RunFullProccesFromAliEnToDataSet","Error in OpenFilesMain(\"async\") !!!"); return retVal; }
  
  tmpOutputTxt_OK.ReplaceAll("_OK.txt","_OK_OK.txt");
  retVal = CreateDataSetFromTxt(tmpOutputTxt_OK.Data(),newDsName.Data(),kTRUE,defaultTree.Data(),proof.Data());
  if (retVal) { Error("RunFullProccesFromAliEnToDataSet","Error in CreateDataSetFromTxt(\"async\") !!!"); return retVal; }
  gProof->VerifyDataSet(newDsName.Data());
  
  gSystem->Exec(Form("rm %s &> /dev/null",tmpTxt.Data()));
  
  return 0;
}

Int_t DownloadDataToProofFromRunListData(TString runListFile="myRunList.txt",Bool_t useFastDownload=kFALSE,TString sRunPeriod="LHC09d",  Int_t passNumber=4) {
  
  
  TString alienDirSheme1 = Form("/alice/data/2009/%s/",sRunPeriod.Data());
  TString alienDirSheme2 = Form("/ESDs/pass%d/",passNumber);
  TString dsName;
  ifstream fileIn;
  fileIn.open(runListFile.Data());

  TString file;
  TString mydir;
  while (fileIn.good()) {
    fileIn >> file;
    if (file.IsNull()) continue;
    mydir = Form("%s%09d%s",alienDirSheme1.Data(),file.Atoi(),alienDirSheme2.Data());
    dsName = Form("%s_%09d_p%d",sRunPeriod.Data(),file.Atoi(),passNumber);
    Info("DownloadDataToProofFromRunList",Form("Downloading direcotry %s with dataset name %s ...",mydir.Data(),dsName.Data()));
    if (RunFullProccesFromAliEnToDataSet(mydir.Data(),dsName.Data(),useFastDownload)) return 1;
  }

  return 0;
}

Int_t DownloadDataToProofFromRunListMC(TString runListFile="myRunList.txt",Bool_t useFastDownload=kFALSE,TString sRunPeriod="LHC10a14") {
  
  
  TString alienDirSheme1 = Form("/alice/sim/%s/",sRunPeriod.Data());
  TString alienDirSheme2 = Form("");
  TString dsName;
  ifstream fileIn;
  fileIn.open(runListFile.Data());

  TString file;
  TString mydir;
  while (fileIn.good()) {
    fileIn >> file;
    if (file.IsNull()) continue;
    mydir = Form("%s%d%s",alienDirSheme1.Data(),file.Atoi(),alienDirSheme2.Data());
    dsName = Form("MC_%s_%d",sRunPeriod.Data(),file.Atoi());
    Info("DownloadDataToProofFromRunList",Form("Downloading direcotry %s with dataset name %s ...",mydir.Data(),dsName.Data()));
    if (RunFullProccesFromAliEnToDataSet(mydir.Data(),dsName.Data(),useFastDownload)) return 1;
  }

  return 0;
}

Int_t RunFullProccesFromAliEnToDataSet(TString aliendirs="/alice/sim/LHC09d10/104870/,/alice/sim/LHC09d10/104871/", TString datasetName="test",Bool_t useFastDownload=kTRUE,TString proof = "aliceskaf@skaf.saske.sk",TString redirectorName = "prf001-iep-grid.saske.sk",TString zipfile="root_archive.zip",TString rootfilename="#AliESDs.root",TString defaultTree="esdTree") {
  
  // init return value
  Int_t retVal=0;
  
  // create dataset from alien
//   TString proof = "aliceskaf@skaf.saske.sk";
//   TString redirectorName = "prf001-iep-grid.saske.sk";
//   TString datasetName = "LHC09d_000104044_p3";
//   TString aliendirs = "/alice/data/2009/LHC09d/000104044/ESDs/pass3/";
//   aliendirs = "/alice/sim/LHC09d10/104870/,/alice/sim/LHC09d10/104871/";
  
//   TString zipfile="root_archive.zip";
//   TString rootfilename="#AliESDs.root";
//   TString defaultTree="esdTree";
  
  TString tmpOutputTxt = Form("%s.txt",datasetName.Data());
  Int_t sleepTime = 10;
  Bool_t forceOverWriteDataset = kFALSE;
  forceOverWriteDataset = kTRUE;
  
  Info("RunFullProccesFromAliEnToDataSet","CreateDataSetFromAliEn ...");
  retVal = CreateDataSetFromAliEn (datasetName.Data(),aliendirs.Data(),forceOverWriteDataset,kFALSE,zipfile.Data(),rootfilename.Data(),defaultTree.Data(),proof.Data(),redirectorName.Data());
  if (retVal) { Error("RunFullProccesFromAliEnToDataSet","Error in CreateDataSetFromAliEn !!!"); return retVal; }
  
  // Download files from dataset
  Info("RunFullProccesFromAliEnToDataSet","CreateTxtFromDataSet ...");
  retVal = CreateTxtFromDataSet(tmpOutputTxt.Data(),datasetName.Data(),proof.Data());
  if (retVal) { Error("RunFullProccesFromAliEnToDataSet","Error in CreateTxtFromDataSet !!!"); return retVal; }

  if (useFastDownload) {
    Info("RunFullProccesFromAliEnToDataSet","OpenFilesMain(async) ...");
    retVal = OpenFilesMain(tmpOutputTxt.Data(),"async");
    if (retVal) { Error("RunFullProccesFromAliEnToDataSet","Error in OpenFilesMain(\"async\") !!!"); return retVal; }
    
    gSystem->Sleep(sleepTime);
  }

  // only OK files are chcecked last time
  TString tmpOutputTxt_OK= tmpOutputTxt.Data();
  tmpOutputTxt_OK.ReplaceAll(".txt","");
  tmpOutputTxt_OK.Append("_OK.txt");

  Info("RunFullProccesFromAliEnToDataSet","OpenFilesMain(Normal) ...");
  retVal = OpenFilesMain(tmpOutputTxt_OK.Data(),"normal");
  if (retVal) { Error("RunFullProccesFromAliEnToDataSet","Error in OpenFilesMain(\"async\") !!!"); return retVal; }
  
  tmpOutputTxt_OK.ReplaceAll("_OK.txt","_OK_OK.txt");
  retVal = CreateDataSetFromTxt(tmpOutputTxt_OK.Data(),datasetName.Data(),kTRUE,defaultTree.Data(),proof.Data());
  if (retVal) { Error("RunFullProccesFromAliEnToDataSet","Error in CreateDataSetFromTxt(\"async\") !!!"); return retVal; }
  gProof->VerifyDataSet(datasetName.Data());
  
  return retVal;
}

Int_t RemoveDataSetAndData(TString datasetname,TString proof="aliceskaf@skaf.saske.sk",TString redirecotr="prf001-iep-grid.saske.sk") {

  Int_t ret=0;
  
  TString dataSetNameSimple = gSystem->BaseName(datasetname.Data());
  if (CreateTxtFromDataSet(Form("%s.txt",dataSetNameSimple.Data()),datasetname.Data(),proof.Data())) return 1;
  
  if (RemoveXrdDataFromTxt(Form("%s.txt",dataSetNameSimple.Data()),kTRUE,redirecotr.Data())) return 2;

  gProof->RemoveDataSet(dataSetNameSimple.Data());
  
  return ret;
}

Int_t RenameDataSet(TString dsFrom,TString dsTo,TString proof="aliceskaf@skaf.saske.sk") {

  Warrning("RenameDataSet","Not Working. Not Tested ...");
//   if (ConnectToProof(proof.Data())) reurn 1;
//   
//   TFileCollection *coll = gProof->GetDataSet(dsFrom.Data());
//   if (!coll) {
//     Error("CreateTxtFromDataSet","coll is null");
//     return 2;
//   }
//   gProof->RemoveDataSet(dsFrom.Data());
//   gProof->RegisterDataSet(dsTo.Data(),coll);
// 
//   gProof->VerifyDataSet(dsTo.Data());
  return 0;
}

Int_t SumDataSetsAllRunsFromTxt(TString filename="allRuns.txt",TString newDataSet="sumtest",TString proof="aliceskaf@skaf.saske.sk") {
  
  TString sRunPeriod="LHC09d";
  Int_t passNumber=3;
  
  TString alienDirSheme1 = Form("/ALICE/pp000900/%s_",sRunPeriod.Data());
  TString alienDirSheme2 = Form("_p%d",passNumber);
  TString dsName;
  ifstream fileIn;
  fileIn.open(filename.Data());

  TString file;
  TString mydir;
  while (fileIn.good()) {
    fileIn >> file;
    if (file.IsNull()) continue;
    dsName.Append(Form("%s%09d%s,",alienDirSheme1.Data(),file.Atoi(),alienDirSheme2.Data()));


  }
  Info("SumDataSetsAllRunsFromTxt",Form("%s",dsName.Data()));
  if (SumDataSets(dsName.Data(),newDataSet.Data(),proof.Data())) return 1;
  return 0;
}

Int_t SumDataSets(TString inDataSets="/ALICE/pp000900/LHC09d_000104800_p4,/ALICE/pp000900/LHC09d_000104801_p4",TString newDataSet="sumtest",TString proof="aliceskaf@skaf.saske.sk") {
  
  Int_t currError=1;
  if (inDataSets.IsNull()) return currError++;
  
  if (ConnectToProof(proof.Data())) return currError++;
  
  gSystem->Exec(Form("rm %s.txt &> /dev/null",newDataSet.Data()));
  
  TFileCollection *collTest = gProof->GetDataSet(newDataSet.Data());
  if (collTest) {
    gProof->RemoveDataSet(newDataSet.Data());
  }
  
  TFileCollection coll;
  TObjArray* array = inDataSets.Tokenize(",");
  TObjString *str;
  TString strr,strrSimple;
  for (Int_t i = 0;i < array->GetEntriesFast();i++) {
      str = (TObjString *) array->At(i);
      strr = str->GetString(); 
      if (strr.IsNull()) continue;
      Info("",strr.Data());
      strrSimple = strr.Remove(0,strr.Last('/')+1);
//       Info("",strrSimple.Data());
      if (CreateTxtFromDataSet(Form("%s.txt",newDataSet.Data()),strr.Data(),proof.Data(),kTRUE,1)) continue;
      coll.AddFromFile(Form("%s.txt",newDataSet.Data()));
  }
  
  gSystem->Exec(Form("rm %s.txt &> /dev/null",newDataSet.Data()));
  
  gProof->RegisterDataSet(newDataSet.Data(),&coll);
  gProof->VerifyDataSet(newDataSet.Data());
  
  return 0;
}

Int_t ConnectToProof(TString proof="aliceskaf@skaf.saske.sk") {
  
//   if (!gProof) {
    gEnv->SetValue("XSec.GSI.DelegProxy", "2");
    TProof::Open(proof.Data(),"masteronly");
    if (!gProof) {
      Error("",Form("Error connecting to %s !!!",proof.Data()));
      return 2;
    }
//   }
  return 0;
}

Int_t CreateDataSetFromAliEn(TString datasetname="test",TString aliendirs="/alice/sim/LHC09d10/104871/,/alice/sim/LHC09d10/104870/",Bool_t forceOverWriteDataset=kFALSE,Bool_t trustInfo=kFALSE, TString zipfile="root_archive.zip", TString rootfilename="#AliESDs.root",TString defaultTree="esdTree",TString proof="aliceskaf@prf001-iep-grid.saske.sk", TString redirectorname="prf001-iep-grid.saske.sk") {
  Int_t ret = 0;
  TString myTmpFileName = Form("tmp_%s.txt", datasetname.Data());
  if (!aliendirs.IsNull()) {
  
    // chcek for alien
    TGrid::Connect("alien://");
    if (!gGrid) {
      Error("","Error connecting to AliEn !!!");
      return 1;
    }
    ofstream tmpFileList(myTmpFileName.Data());
    if (!tmpFileList) {
      Error("StageFromAliEnToProof", Form("Cannot open file %s", myTmpFileName.Data()));
      return kFALSE;
    }
  
    TAlienResult* gr;
    TObjArray* array = aliendirs.Tokenize(",");
    TObjString *str;
    TString strr,tmpfilename,tmpfileline;
    for (Int_t i = 0;i < array->GetEntriesFast();i++) {
      str = (TObjString *) array->At(i);
      strr = str->GetString();    
      Info("",strr.Data());
      gr = (TAlienResult*) gGrid->Command(Form("find %s/* %s",strr.Data(),zipfile.Data()));
      for (Int_t iEntry = 0;iEntry < gr->GetEntries();iEntry++) {
        tmpfilename = gr->GetKey(iEntry,"lfn");
        tmpfilename = Form("root://%s/%s%s",redirectorname.Data(),tmpfilename.Data(),rootfilename.Data());
        Printf(tmpfilename.Data());
        tmpFileList << tmpfilename << endl;
      }
    }
  
    tmpFileList.close();
  }
  else {
    myTmpFileName.ReplaceAll("tmp_","");
  }
  
  TFileCollection filecollection;
  filecollection.AddFromFile(myTmpFileName.Data());

//   // connect to proof cluster
  ret = ConnectToProof(proof.Data());
  if (ret) return ret;
  
  if (forceOverWriteDataset) gProof->RemoveDataSet(datasetname.Data());
  
  TString extraParameters;
  if (forceOverWriteDataset) extraParameters+="O";
  if (trustInfo) extraParameters+="T";
  
  Info("CreateDataSetFromAliEn",Form("Dataset %s registred with options : %s",datasetname.Data(),extraParameters.Data()));
  gProof->RegisterDataSet(datasetname.Data(),&filecollection,extraParameters.Data());

  if (!defaultTree.IsNull())
    gProof->SetDataSetTreeName(datasetname.Data(),defaultTree.Data());
  
//   gSystem->Rm(Form("tmp_%s.txt", datasetname.Data()));
  
  return 0;
}

Int_t CreateDataSetFromTxt(TString myfile="myfile.txt",TString datasetname="test",Bool_t forceOverWriteDataset=kFALSE,TString defaultTree="esdTree",TString proof="aliceskaf@prf001-iep-grid.saske.sk") {
  Int_t ret = 0;
  
  TFileCollection filecollection;
  filecollection.AddFromFile(myfile.Data());

//   // connect to proof cluster
  ret = ConnectToProof(proof.Data());
  if (ret) return ret;
  
  if (forceOverWriteDataset) gProof->RemoveDataSet(datasetname.Data());
  
  gProof->RegisterDataSet(datasetname.Data(),&filecollection);

  if (!defaultTree.IsNull())
    gProof->SetDataSetTreeName(datasetname.Data(),defaultTree.Data());
  
//   gSystem->Rm(myTmpFileName.Data());
  return 0;
}


Int_t CreateTxtFromDataSet(TString filename="myfile.txt",TString dataSetName="",TString proof="aliceskaf@skaf.saske.sk",TString redirector="",Bool_t forceDeleteOutputfileFirst=kTRUE,Int_t fileInfoNum=0) {
  
  Int_t ret = 0;    
  ret = ConnectToProof(proof.Data());
  if (ret) return ret;
  
  if (dataSetName.IsNull()) {
    gProof->ShowDataSets();
    return 1;
  }
  
  TFileCollection *coll = gProof->GetDataSet(dataSetName.Data());
  if (!coll) {
    Error("CreateTxtFromDataSet","coll is null");
    return 2;
  }

  if (forceDeleteOutputfileFirst)
    gSystem->Exec(Form("rm -f %s &> /dev/null", filename.Data()));
  ofstream outfiles(Form("%s", filename.Data()),ios::app);
  if (!outfiles.is_open()) {
    Error("CreateTxtFromDataSet", Form("Cannot open file %s", filename.Data()));
    return 3;
  }


   TIter iter(coll->GetList());
   TFileInfo *fileInfo = 0;
   Long64_t nFiles=0;
   Long64_t numOfFiles =  coll->GetNFiles();
   TUrl *u = 0;
   Int_t tmpCouter=0;
   TString tmpFileName;
   while ((fileInfo = dynamic_cast<TFileInfo*>(iter.Next()))) {
     
//      printf("\rProcessing (%d of %d) ...",++nFiles,numOfFiles);
//      if (fileInfo->TestBit(fileInfo->kStaged)){
         
      fileInfo->ResetUrl();
      tmpCouter=0;
      while ((u = (TUrl*)fileInfo->NextUrl())){
        if (tmpCouter++==fileInfoNum) {
          tmpFileName = u->GetUrl();
	  if (redirector.IsNull()){
	    tmpFileName.ReplaceAll(":1095","");
//           tmpFileName.ReplaceAll("root://","");
//           tmpFileName.ReplaceAll("//"," /");
//           tmpFileName.Remove(tmpFileName.Last('#'),tmpFileName.Length());
	    outfiles << tmpFileName.Data() << endl;
	  } else {
	    tmpFileName.ReplaceAll(":1095","");
//           tmpFileName.ReplaceAll("root://","");
	    tmpFileName.ReplaceAll("//","?");
	    tmpFileName.Remove(0,tmpFileName.Last('?'));
	    tmpFileName.ReplaceAll("?",Form("root://%s//",redirector.Data()));
	    outfiles << tmpFileName.Data() << endl;
	  }
        }
      }
//      }

     
  }
  cout << endl;
  outfiles.close();
  
  return 0;
}

Int_t PrintDataSet(TString dataSetName="",TString proof="aliceskaf@skaf.saske.sk",Int_t fileInfoNum=0) {
  
  Int_t ret = 0;    
  ret = ConnectToProof(proof.Data());
  if (ret) return ret;
  
  if (dataSetName.IsNull()) {
    gProof->ShowDataSets();
    return 1;
  }
  
  TFileCollection *coll = gProof->GetDataSet(dataSetName.Data());
  if (!coll) {
    Error("CreateTxtFromDataSet","coll is null");
    return 2;
  }

   TIter iter(coll->GetList());
   TFileInfo *fileInfo = 0;
   Long64_t nFiles=0;
   Long64_t numOfFiles =  coll->GetNFiles();
   TUrl *u = 0;
   Int_t tmpCouter=0;
   TString tmpFileName;
   while ((fileInfo = dynamic_cast<TFileInfo*>(iter.Next()))) {
     
//      printf("\rProcessing (%d of %d) ...",++nFiles,numOfFiles);
//      if (fileInfo->TestBit(fileInfo->kStaged)){
         
      fileInfo->ResetUrl();
      tmpCouter=0;
      while ((u = (TUrl*)fileInfo->NextUrl())){
        if (tmpCouter++==fileInfoNum) {
          tmpFileName = u->GetUrl();
/*	  if (redirector.IsNull()){
	    tmpFileName.ReplaceAll(":1095","");
//           tmpFileName.ReplaceAll("root://","");
//           tmpFileName.ReplaceAll("//"," /");
//           tmpFileName.Remove(tmpFileName.Last('#'),tmpFileName.Length());
	    outfiles << tmpFileName.Data() << endl;
	  } else {
	    tmpFileName.ReplaceAll(":1095","");
//           tmpFileName.ReplaceAll("root://","");
	    tmpFileName.ReplaceAll("//","?");
	    tmpFileName.Remove(0,tmpFileName.Last('?'));
	    tmpFileName.ReplaceAll("?",Form("root://%s//",redirector.Data()));
	    outfiles << tmpFileName.Data() << endl;
	  }*/
	  Printf(Form("%s",tmpFileName.Data()));
        }
      }
//      }

     
  }
//   cout << endl;
//   outfiles.close();
  
  return 0;
}


Int_t OpenFilesMain(TString inputFileName,TString mode="normal" /*or async*/) {
  
  TStopwatch timer;
  timer.Start();

  // if input is xml, then convert it in to txt
  if (inputFileName.EndsWith(".xml"))
    if (ConvertXML2TXT(inputFileName, redirectorName, xmlAppend) < 0) return -1;

  inputFileName.ReplaceAll(".xml", ".txt");
  inputFileName.ReplaceAll(".txt", "");

  CleanOKAndBadFiles(inputFileName);

  ofstream fileOKFiles(Form("%s_OK.txt", inputFileName.Data()));
  if (!fileOKFiles) {
    Error("OpenFilesMain", Form("Cannot open file %s", inputFileName.Data()));
    return kFALSE;
  }

  ofstream fileBadFiles(Form("%s_BAD.txt", inputFileName.Data()));
  if (!fileBadFiles) {
    Error("OpenFilesMain", Form("Cannot open file %s", inputFileName.Data()));
    return kFALSE;
  }

  if (!mode.CompareTo("async"))
    OpenFilesAsync(inputFileName, fileOKFiles, fileBadFiles);
  else
    OpenFilesNormal(inputFileName, fileOKFiles, fileBadFiles);

  fileOKFiles.close();
  fileBadFiles.close();

  timer.Stop();
  timer.Print();

  return 0;
}

void CleanOKAndBadFiles(TString fileName)
{
  gSystem->Exec(Form("rm -f %s_OK.txt", fileName.Data()));
  gSystem->Exec(Form("rm -f %s_BAD.txt", fileName.Data()));
}

Int_t ConvertXML2TXT(TString xmlFile = "my.xml", TString redirectorName = "prf001-iep-grid.saske.sk", TString xmlAppend = "AliESDs.root")
{

  // handle alien lib

  // open alien collection
  TAlienCollection *myCollection = TAlienCollection::Open(xmlFile.Data());
  if (!myCollection) {
    Error("ConvertXML2TXT", Form("Cannot open AliEn collection %s !!!", xmlFile.Data()));
    return -1;
  }

  // open output file with txt extension
  TString outFilename = xmlFile.Data();
  outFilename.ReplaceAll("xml", "txt");
  ofstream file(outFilename, ios::out);

  if (!file.is_open()) {
    Error("ConvertXML2TXT", Form("File %s was not created !!!", outFilename.Data()));
    return -2;
  }

  // initialize a counter to check the number of read files
  UInt_t numOfFiles = myCollection->GetNofGroups();

  TString filename;

  while (myCollection->Next()) {
    filename = myCollection->GetTURL("");
    filename.ReplaceAll("alien:///", Form("root://%s//", redirectorName.Data()));
    filename += "#";
    filename += xmlAppend.Data();
    file << filename.Data() << endl;
  }

  file.close();

  Info("ConvertXML2TXT", Form("Converted %s to %s ...", xmlFile.Data(), outFilename.Data()));

  return 0;
}

Int_t OpenFilesNormal(TString filename, ofstream & fileOk, ofstream & fileBad)
{

  ifstream fileIn;
  fileIn.open(Form("%s.txt", filename.Data()));
  Printf("Starting Normal ...");
  Int_t numOk = 0, numBad = 0;
  TFile *f;
  TString file;
  while (fileIn.good()) {
    fileIn >> file;

    if (file.IsNull()) continue;

    printf(Form("\rOpening Normal %s%c", file.Data(),3));
    f = TFile::Open(file.Data());
    if (f) {
      fileOk << file.Data() << endl;
      numOk++;
    }
    else {
      fileBad << file.Data() << endl;
      numBad++;
    }

    delete f;
  }

  fileIn.close();
  Printf("Normal done ...");
  Info("OpenFilesNormal", Form("Bad files : %d out of %.0f", numBad, (Double_t)numBad + numOk));
  return 0;
  
}
Int_t OpenFilesAsync(TString filename, ofstream & fileOk, ofstream & fileBad, Int_t numberFilesOpenedAtOnce = 100)
{

  gSystem->Load("libNetx");
  ifstream fileIn;
  fileIn.open(Form("%s.txt", filename.Data()));

  TObjArray array;
  TFileOpenHandle *foh = 0;
  Printf("Starting Async ...");
  Int_t numOk = 0, numBad = 0;
  TFile *f;
  TString file;
  while (fileIn.good()) {
    fileIn >> file;
    if (file.IsNull()) continue;
    printf(Form("\rOpening Async %s%c", file.Data(), 3));
    foh = TFile::AsyncOpen(file.Data());
    if (foh)
      array.Add((TObject*)foh);
  }
  Printf("Async done ...");
  Printf("Starting Async2 ...");
  fileIn.close();

  TIter myiter(&array);
  while ((foh = (TFileOpenHandle *)myiter.Next())) {
    if (foh) {
      f = TFile::Open(foh);
      if (f) {
        printf(Form("\rOpening Async2 %s%c", f->GetName(), 3));
        fileOk << f->GetName() << endl;
        numOk++;
      }
      else {
        fileBad << f->GetName() << endl;
        numBad++;
      }
    }
  }
  Printf("Async2 done ...");
  Info("OpenFilesAsync", Form("Bad files : %d out of %.0f", numBad, (Double_t)numBad + numOk));

  return 0;
}

Int_t RemoveXrdDataFromTxt(TString filename="myfile.txt",Bool_t isOneFileOneDirectory=kFALSE,TString redirecotr="prf001-iep-grid.saske.sk",  Bool_t useXrdStageTools = kFALSE) {

//   useXrdStageTools = kTRUE;
  Int_t sleepSec = 5;
  Int_t xargsNum = 20;
  
  TStopwatch timerAll,timer;
  timerAll.Start();
  
  TString fileline,strr;
  ifstream fileIn;
  fileIn.open(filename.Data());
  
  TString xrdSTName = Form("xrdST_%s.txt", filename.Data());
  gSystem->Exec(Form("rm -f %s &> /dev/null", xrdSTName.Data()));
  ofstream tmpXrdST(Form("%s", xrdSTName.Data()));
  if (!tmpXrdST) {
    Error("OpenFilesMain", Form("Cannot open file %s",xrdSTName.Data()));
    return 1;
  }
  Long64_t numLines=0;
  while (fileIn.good()) {
    fileIn >> fileline;
    if (fileline.IsNull()) continue;
    strr = GetXrdFileName(fileline);
//     Info("",Form("root://%s/%s",redirecotr.Data(),strr.Data()));
    tmpXrdST << Form("root://%s/%s",redirecotr.Data(),strr.Data()) << endl;
    numLines++;
  }
  
  tmpXrdST.close();
//   return 0;
//   
  if (useXrdStageTools) {
    Info("",Form("cat %s | xargs -n %d xrdstagetool -p ",xrdSTName.Data(),xargsNum));
    Int_t i;
    cout << "WARRNING: You are going to run xrdstagetools!!! Press 0 if you don't know what you are doing.\nEnter value: ";
    cin >> i;
    if (i==100) {
    gSystem->Exec(Form("cat %s | xargs -n %d xrdstagetool -p ",xrdSTName.Data(),xargsNum));
    } else {
      return 2;
    }
//     gSystem->Exec(Form("cat %s | xargs -n %d echo ",xrdSTName.Data(),xargsNum));
    
    Info("",Form("Sleeping %d sec",sleepSec));
    gSystem->Sleep(sleepSec*1000);
  }
//   return 0;
  
  //remove file
  gSystem->Exec(Form("rm -f xrdST_%s.txt &> /dev/null", filename.Data()));
  
  fileIn.close();
  
  fileIn.open(filename.Data());
//   numLines--;

  TObjArray* array;
  TObjString *str;
  TString tmpfilename,tmpfileline;
  Int_t retval;
  Long64_t numCurrLine=0;
  while (fileIn.good()) {
    numCurrLine++;
    timer.Reset();
    timer.Start();
    
    fileIn >> fileline;
    if (fileline.IsNull()) continue;
    
    strr = GetXrdFileName(fileline);
    Info("RemoveXrdDataFromTxt",Form("(%d of %d)Removing %s",numCurrLine,numLines,strr.Data()));
    retval =gSystem->Exec(Form("xrd %s rm %s &> /dev/null",redirecotr.Data(),strr.Data()));
    strr.Remove(strr.Last('/'),strr.Length());
//     Info("RemoveXrdDataFromTxt",Form("%s",strr.Data()));
    if ((isOneFileOneDirectory)&&(retval)) ){
      Info("RemoveXrdDataFromTxt",Form("(%d of %d)Removing %s/DIR_LOCK",numCurrLine,numLines,strr.Data()));
      gSystem->Exec(Form("xrd %s rm %s/DIR_LOCK &> /dev/null",redirecotr.Data(),strr.Data()));
      gSystem->Exec(Form("xrd %s rmdir %s &> /dev/null",redirecotr.Data(),strr.Data()));
    }
    timer.Stop();
    timer.Print();
  }

  fileIn.close();
  timerAll.Stop();
  timerAll.Print();
}

TString GetXrdFileName(TString file){
  
    
    file.ReplaceAll(":1095","");
    file.ReplaceAll("root://","");
    file.ReplaceAll("//"," /");
    file.Remove(file.Last('#'),file.Length());

    TObjArray* array = file.Tokenize(" ");
    TObjString *str = (TObjString *) array->At(1);
    TString strr = str->GetString(); 
  
  
  return strr;
}